var areaList = [];
var deliveryManList = [];

$("#dop_datagrid_storefront_relate").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : true,
	fit : false,
	pageSize : 10,
	pageList : [10, 30, 50, 100],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [[]],
	columns : [[{
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "stoName",
		width : 150,
		title : "店面名称",
		halign:"center",
		align:'left'
	}, {
		field : "stoAddress",
		width : 350,
		title : "店面地址",
		halign:"center",
		align:'left'
	}, {
		field : "restaurantId",
		hidden : true,
		title : "所属商户",
		align:'center'
	}, {
		field : "restaurantName",
		width : 150,
		title : "所属商户",
		halign:"center",
		align:'left'
	}, {
		field : "areaIdList",
		width : 150,
		title : "已关联区域ID",
		hidden : true
	}, {
		field : "areaName",
		width : 500,
		title : "区域名称",
		halign:"center",
		align:'left'
	}, {
		field : "deliveryManIdList",
		width : 150,
		title : "已关联人员ID",
		hidden : true
	}, {
		field : "deliverymanName",
		width : 500,
		title : "人员姓名",
		halign:"center",
		align:'left'
	}]]
});

$(function(){
	initPage();
	init();
});

function initPage(){
	var p = $("#dop_datagrid_storefront_relate").datagrid('getPager');
	$(p).pagination(
		{
			onSelectPage : function(pageNumber, pageSize) {
				var params = $.extend({
					pageNum : pageNumber,
					pageSize : pageSize
				}, getQueryParams());
				EapTools.doAction("storefrontController.do?queryList", params,
						true, function(data) {
							$("#dop_datagrid_storefront_relate").datagrid(
									'loadData', {
										"rows" : data.list,
										"total" : data.count
									} || {});
						});
			}
		});
}


// bind assign event
$("#dop_btn_storefront_assign")
		.click(
				function() {
					var selections = $("#dop_datagrid_storefront_relate")
							.datagrid("getSelections");
					if (selections && selections.length > 0) {
						if (selections.length > 1) {
							layer.tips('请选择一行数据', this, {
								guide : 2,
								time : 2
							});
						} else {
							var selectedAreaId = $("#area").val();
							var deliverymanId = $("#deliveryman").val();
							EapTools
									.doAction(
											"storefrontRelateController.do?assignAreaAndDeliveryman",
											{
												id : $(
														"#dop_datagrid_storefront_relate")
														.datagrid(
																"getSelections")[0].id,
												areaIdList : selectedAreaId,
												deliverymanIdList : deliverymanId
											}, true, function(data) {
												if (data.success) {
													EapTip.msgOk("分配成功");
												} else {
													EapTip.msgError("分配失败");
												}
												init();
											});
						}
					} else
						layer.tips('请选择要修改行', this, {
							guide : 2,
							time : 2
						});
				});

// bind query event
$("#dop_btn_storefront_query").click(function() {
	init();
});

// get params
function getQueryParams() {
	return {
		stoName : $("input[code='stoName']").val(),
		stoAddress : $("input[code='stoAddress']").val(),
		restaurantId : $("select[code='restaurantId']").val()
	};
}

//绑定商户列表
EapTools.doAction("restaurantController.do?findData", {}, true, function(data) {
	var selectorArray = [];
	selectorArray.push("<option value=''>---请选择---</option>");
	for (var i = 0; i < data.length; i++) {
		var thisObj = data[i];
		selectorArray.push("<option value='", thisObj.id, "'>", thisObj.resName, "</option>");
	}
	$("select[code='restaurantId']").append(selectorArray.join(""));
	$("select[id='restaurantId']").append(selectorArray.join(""));
});

// init
function init() {
	// init
	var options = $("#dop_datagrid_storefront_relate").datagrid('getPager')
			.data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("storefrontController.do?queryList", params, true,
			function(data) {
				$("#dop_datagrid_storefront_relate").datagrid('loadData', {
					"rows" : data.list,
					"total" : data.count
				} || {});
				var p = $("#dop_datagrid_storefront_relate").datagrid(
						'getPager');
				$("td:last", p).find("a").click();
			});
}

$(function(){
	
	// 查询区域信息
	EapTools.doAction("areaController.do?findData", {},
			true, function(data) {
				areaList = data;
				var areaSelect=$("#area");
				// 区域
				for (var i = 0; i < data.length; i++) {
					areaSelect.append(
							"<option value='"
									+ data[i].id
									+ "'>"
									+ data[i].baseAreaName
									+ "</option>");
				}
				areaSelect.multipleSelect({
					filter : true,
					selectAll: false
				});
			});
	// 查询快递员信息
	EapTools.doAction("deliveryManController.do?findData", {}, true, function(
			data) {
			deliveryManList = data;
			var deliveryman=$("#deliveryman");
			for (var i = 0; i < data.length; i++) {
				deliveryman.append(
						"<option value='"
								+ data[i].id
								+ "'>"
								+ data[i].delName
								+ "</option>");
			}
			deliveryman.multipleSelect({
				filter : true,
				selectAll: false
			});
	});
	
});


// 行切换
$("#dop_datagrid_storefront_relate").datagrid({
					onClickRow : function() {
						var selections = $("#dop_datagrid_storefront_relate")
								.datagrid("getSelections");
						var storefrontId = selections[0].id;
						var Grid_areaList = selections[0].areaIdList 
						var Grid_deliverymanIdList =  selections[0].deliveryManIdList;
						$("#area").multipleSelect("setSelects", Grid_areaList);				
						$("#deliveryman").multipleSelect("setSelects", Grid_deliverymanIdList);						
					}
				});
